#include "Common.h"
#include "SystemV/AppLibSystemV_Msg.hpp"

void Flush(void)
{
    char c;

    while((c = getchar()) != '\n' && c != EOF);
}

int main(int argc, char **args)
{
	SINT32 msgId = -1;
    SINT32 iRet = 0;
	ULONG uRet = STAT_SUCCESS;
	CHAR szBuff[1024] = {0}; 
	APP_LIB_SYSTEM_V_MSG_s *pstMsg = (APP_LIB_SYSTEM_V_MSG_s *)szBuff;

	if (argc != 2)
	{
		printf("Usage: ./SystemV_Msg <Path>");
		return -1;
	}

	uRet = AppLibSystemV_MsgCreate(args[1], &msgId);
	if (uRet != STAT_SUCCESS)
	{
		printf("[%s %d] AppLibSystemV_MsgCreate ret=%lu\n", __func__, __LINE__, uRet);
		return -2;
	}

	while (1)
	{
		memset(szBuff, 0, sizeof(szBuff));
		
		iRet = scanf("%s", (CHAR *)pstMsg->auData);
        UNUSED(iRet);
		Flush();

		pstMsg->lType = 1;
		pstMsg->usLen = strlen((CHAR *)pstMsg->auData) + 1;
		uRet = AppLibSystemV_MsgSendWait(msgId, pstMsg);
		if (uRet != STAT_SUCCESS)
		{
			continue;
		}

		printf("[%s %d] Send Data <%s> Successfully\n", __func__, __LINE__, (char *)pstMsg->auData);
		if (strcmp((char *)pstMsg->auData, "quit") == 0)
		{
			break;
		}
	}

	return 0;
}
